/**
 * 两周内自动登陆拦截器
 * @author gaocc 
 * @date 2014-10-11
 */
package com.chnbst.bstyun.interceptor;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.service.ShoppingCartService;
import com.chnbst.bstyun.service.UserService;

public class AutoLoginInterceptor extends HandlerInterceptorAdapter{
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private UserService userService;
	@Autowired
	private ShoppingCartService shoppingCartService;
	@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String userName = null;
		String userPassword = null;
//		String servletPath = request.getServletPath();
//		if (servletPath.equals("/")) {
//			
//		}
		Cookie[] cookies = request.getCookies();
		if (cookies!=null&&cookies.length!=0) {
			for (Cookie cookie : cookies) {
				if(cookie.getName().equals("userName")){
					userName = cookie.getValue();
				}
				if(cookie.getName().equals("userPassword")){
					userPassword = cookie.getValue();
				}
			}
			if (userName!=null&&userPassword!=null) {
				String loginWay = null;
				User user = new User();
				if (userName.indexOf("@")==-1) {
					user.setUserTel(userName);
					loginWay = "tel";
				}else {
					user.setUserEmail(userName);
					loginWay = "email";
				}
				user = (User)userService.selectUserByAccount(user);
				if (user!=null) {
					if (user.getUserPassword().equals(userPassword)) {
						request.getSession().setAttribute("userInfo", user);
						request.getSession().setAttribute("loginWay", loginWay);
						
//						Map<String, Object> map = new HashMap<String, Object>();
//						map.put("isFree", 1);
//						map.put("userId", loginUser.getUserId());
//						List<Order> orders = orderService.selectOrdersByMap(map);
//						if (orders.size() > 0) {
//							request.getSession().setAttribute("isFree", 1);
//						} else {
//							request.getSession().setAttribute("isFree", 0);
//						}
						
						Map<String, Object> cartInfo = shoppingCartService.selectShoppingCartCountByUserId(user.getUserId());
						if(cartInfo.get("sumPrice") == null) cartInfo.put("sumPrice", 0.0);
						request.getSession().setAttribute("cartInfo", cartInfo);
//						User newUser = new User();
//						newUser.setUserId(user.getUserId());
//						newUser.setLastLoginTime(new Date());
//						userService.updateUserLastTime(newUser);
					}
				}
			}
		}
		return true;
    }
 
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }
}
